package com.birdcage.gamemanagers;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL11;
import com.badlogic.gdx.graphics.glutils.VertexBufferObject;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.utils.BufferUtils;

///Singleton
///This will detect all physics logic of the game.
public class GeometryRenderer
{
    ///Singleton Implement
    static public GeometryRenderer GetInstance()
    {
        if (m_instance == null)
        {
            m_instance = new GeometryRenderer();
        }       
        return m_instance;
    }
    
    private GeometryRenderer()
    {
        m_projectionMatrix.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    };
    private static GeometryRenderer m_instance = null;
    private final float[] m_vertices = new float[100];
    private final float[] m_colors = new float[200];
    private int m_vertexIndex = 0;
    private int m_colorIndex = 0;
    ///Private Member    
    private final Matrix4 m_transformMatrix = new Matrix4();
    private final Matrix4 m_projectionMatrix = new Matrix4();
    
    ///Public Method.
    public void Begin()
    {        
        GL10 gl = Gdx.gl10;         

         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        
        gl.glPushMatrix();
        gl.glLoadIdentity();
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadMatrixf(m_projectionMatrix.val, 0);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadMatrixf(m_transformMatrix.val, 0);        
    }
    
    public void Vertex2(float x, float y)
    {
        m_vertices[m_vertexIndex++] = x;
        m_vertices[m_vertexIndex++] = y;
    }
    
    public void Color4(int r, int g, int b, int a)
    {
        m_colors[m_colorIndex++] = r / 255;
        m_colors[m_colorIndex++] = g / 255;
        m_colors[m_colorIndex++] = b / 255;
        m_colors[m_colorIndex++] = a / 255;
    }
    
    public void End()
    {
        GL10 gl = Gdx.gl10;
        if (m_vertexIndex >= 2)
        {
            if (m_colorIndex > 0)
            {
                ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * m_colorIndex);
                byteBuffer.order(ByteOrder.nativeOrder());
                BufferUtils.copy(m_colors, byteBuffer, m_colorIndex, 0);
                byteBuffer.position(0);
                byteBuffer.limit(m_colorIndex);
                FloatBuffer buffer = byteBuffer.asFloatBuffer();
                gl.glColorPointer(4, gl.GL_FLOAT, 0, buffer);
            }
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * m_vertexIndex);
            byteBuffer.order(ByteOrder.nativeOrder());
            BufferUtils.copy(m_vertices, byteBuffer, m_vertexIndex, 0);
            byteBuffer.position(0);
            byteBuffer.limit(m_vertexIndex);
            FloatBuffer vertexBuffer = byteBuffer.asFloatBuffer();      
            gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertexBuffer);
            
            gl.glDrawArrays(gl.GL_LINE_LOOP,  0, 4);
        }
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glPopMatrix();
        m_vertexIndex = 0;
        m_colorIndex = 0;
    }
    
}
